package com.cms.controllers.admin;

import com.cms.common.aop.permission.PermissionCheck;
import com.cms.common.model.entity.AdminUser;
import com.cms.common.model.entity.Roles;
import com.cms.common.model.services.AdminUserService;
import com.cms.common.model.services.RolesService;
import com.cms.common.model.vo.RequestData;
import com.cms.common.model.vo.ResponseResult;
import com.cms.common.utils.MD5Util;
import com.cms.controllers.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;

@Controller
@RequestMapping("/admin/users")
public class AdminUserController extends BaseController {

    @Autowired
    AdminUserService adminUserService;
    @Autowired
    RolesService rolesService;

    @GetMapping("")
    @PermissionCheck(name = "用户列表", typeName = "用户管理")
    public String index(Model viewModel) {
        viewModel.addAttribute("all_roles", rolesService.getAllRoles());
        return "admin/users/index";
    }

    @GetMapping("/list")
    @ResponseBody
    @PermissionCheck(name = "用户列表数据", typeName = "用户管理")
    public ResponseResult users(@ModelAttribute RequestData requestData) {
        Page<AdminUser> roles = adminUserService.getAdminUserByPage(requestData);
        return ResponseResult.success(roles);
    }

    @PostMapping("/store")
    @ResponseBody
    @PermissionCheck(name = "创建用户", typeName = "用户管理")
    public ResponseResult store(@RequestParam("name") String name, @RequestParam("username") String username, @RequestParam(value = "password") String password, @RequestParam("role_id") Long role_id) {
        if (password.isEmpty()) {
            ResponseResult.fail("密码不能为空");
        }
        AdminUser adminUser = adminUserService.getAdminUserByUsername(username);
        if (adminUser != null) {
            ResponseResult.fail("用户名已存在");
        }
        Roles roles = rolesService.getRolesId(role_id);
        if (roles == null) {
            ResponseResult.fail("角色不存在");
        }
        adminUser = new AdminUser();
        adminUser.setName(name);
        adminUser.setUsername(username);
        String salt = null;
        try {
            salt = MD5Util.MD5Encode(username + password + System.currentTimeMillis());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        adminUser.setSalt(salt);
        adminUser.setPassword(adminUserService.encodePwd(username, password, salt));
        adminUser.setRoles(roles);
        adminUser = adminUserService.insertAdminUser(adminUser);
        return ResponseResult.success(adminUser);
    }

    @PutMapping("/update/{id}")
    @ResponseBody
    @PermissionCheck(name = "修改用户", typeName = "用户管理")
    public ResponseResult update(@PathVariable Long id, @RequestParam("name") String name, @RequestParam("username") String username, @RequestParam(value = "password", defaultValue = "") String password, @RequestParam("role_id") Long role_id) {

        AdminUser adminUser = adminUserService.getAdminUserByUsername(username);
        if (adminUser != null && adminUser.getId() != id) {
            ResponseResult.fail("用户名已存在");
        }
        Roles roles = rolesService.getRolesId(role_id);
        if (roles == null) {
            ResponseResult.fail("角色不存在");
        }
        adminUser = adminUserService.getAdminUserById(id);
        adminUser.setUsername(username);
        adminUser.setName(name);
        if (!password.isEmpty()) {
            adminUser.setPassword(adminUserService.encodePwd(username, password, adminUser.getSalt()));
        }
        adminUser.setRoles(roles);
        adminUser = adminUserService.insertAdminUser(adminUser);
        return ResponseResult.success(adminUser);
    }

    @DeleteMapping("/destroy/{id}")
    @ResponseBody
    @PermissionCheck(name = "删除用户", typeName = "用户管理")
    public ResponseResult destroy(@PathVariable Long id) {
        adminUserService.deleteUsers(adminUserService.getAdminUserById(id));
        return ResponseResult.success("删除成功");
    }
}
